package com.aqin.ocr_demo;

import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.ocr.AipOcr;

// PNG、JPG、JPEG、BMP、GIF**
public class CharacterRecognition {
    static final String APP_ID = "10089095";
    static final String API_KEY = "CjEhRW8sz87xThC7hUv6LdyM";
    static final String SECRET_KEY = "X1tKP1VIuosA2ShqBSQ03hi2PiE3DDhy";
    static final String DOC_ROOT_DIR = "F:/Company/Mine/baidu/ai/ocr/ocr-demo/docs/";
    AipOcr ocr = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

    public CharacterRecognition() {
        // 可选：设置网络连接参数
        ocr.setConnectionTimeoutInMillis(2000); // 建立连接的超时时间（单位：毫秒）
        ocr.setSocketTimeoutInMillis(60000); // 通过打开的连接传输数据的超时时间（单位：毫秒）
    }

    public void doIDCard(String idCardPath) {
        JSONObject res = ocr.idcard(DOC_ROOT_DIR + idCardPath, true, new HashMap<String, String>());
        System.out.println("           调用身份证识别接口");
        System.out.println("----------------------------------------");
        System.out.println(res.toString(2));
    }

    public void doBankCard(String bankCardPath) {
        JSONObject res = ocr.bankcard(DOC_ROOT_DIR + bankCardPath);
        System.out.println("           调用银行卡识别接口");
        System.out.println("----------------------------------------");
        System.out.println(res.toString(2));
    }

    public void doBasicGeneral(String imgUrl) {
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "false");
        options.put("language_type", "CHN_ENG");

        JSONObject res = ocr.basicGeneralUrl(imgUrl, options);
        System.out.println("            调用通用识别接口");
        System.out.println("----------------------------------------");
        System.out.println(res.toString(2));
    }

    public void doGeneral(String imgPath) {
        JSONObject res = ocr.general(DOC_ROOT_DIR + imgPath, new HashMap<String, String>());
        System.out.println("            调用通用识别（含位置信息）接口");
        System.out.println("----------------------------------------");
        System.out.println(res.toString(2));
    }

    public void doDrivingLicense(String licensePath) {
        JSONObject res = ocr.drivingLicense(DOC_ROOT_DIR + licensePath, new HashMap<String, String>());
        System.out.println("            驾驶证识别");
        System.out.println("----------------------------------------");
        System.out.println(res.toString(2));
    }

    public void doVehicleLicense(String licensePath) {
        JSONObject res = ocr.vehicleLicense(licensePath, new HashMap<String, String>());
        System.out.println("            行驶证识别");
        System.out.println("----------------------------------------");
        System.out.println(res.toString(2));
    }

    public void doPlateLicense(String imgPath) {
        JSONObject res = ocr.plateLicense(imgPath);
        System.out.println("            车牌识别");
        System.out.println("----------------------------------------");
        System.out.println(res.toString(2));
    }


    public static void main(String[] args) {

        CharacterRecognition recognition = new CharacterRecognition();
        // recognition.doIDCard("IDCard-0.jpg");
        // recognition.doBankCard("BankCard-0.jpg");
        // recognition.doBasicGeneral(
        // "http://pic.ibaotu.com/00/19/62/21T888piCpFx.jpg-0.jpg!/fw/700/watermark/url/L3RvL2xvZ29fdG8ucG5n/align/center");
        // recognition.doGeneral("Img-0.jpg");

        // recognition.doDrivingLicense("DrivingLicense-0.jpg");
        // recognition.doVehicleLicense("VehicleLicense-0.jpg");
        recognition.doPlateLicense("PlateLicense-0.jpg");
    }
}
